package com.terren.spider.core.html.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.terren.spider.db.dbsource.DBSourceManager;
import com.terren.spider.db.service.AppDataService;
import com.terren.spider.db.service.AppXhsService;
import com.terren.spider.db.service.ArticleSearchService;
import com.terren.spider.db.service.ArticleService;
import com.terren.spider.db.service.ArticleTopicService;
import com.terren.spider.db.service.ChengjiaofangService;
import com.terren.spider.db.service.CommentsService;
import com.terren.spider.db.service.DouyinService;
import com.terren.spider.db.service.ElectronicCommerceService;
import com.terren.spider.db.service.ErshoufangService;
import com.terren.spider.db.service.SpiderKeywordService;
import com.terren.spider.db.service.TiebaUserService;
import com.terren.spider.db.service.ZhihuContentService;
import com.terren.spider.db.service.ZhihuUserService;
import com.terren.spider.entity.biz.AppData;
import com.terren.spider.entity.biz.Article;
import com.terren.spider.entity.biz.ArticleReply;
import com.terren.spider.entity.biz.ArticleSearch;
import com.terren.spider.entity.biz.ArticleTopic;
import com.terren.spider.entity.biz.Chengjiaofang;
import com.terren.spider.entity.biz.DetailXhs;
import com.terren.spider.entity.biz.Douyin;
import com.terren.spider.entity.biz.ElectronicCommerce;
import com.terren.spider.entity.biz.Ershoufang;
import com.terren.spider.entity.biz.TiebaUser;
import com.terren.spider.entity.biz.UnionComments;
import com.terren.spider.entity.biz.UnionUserInfo;
import com.terren.spider.entity.biz.WebTestResult;
import com.terren.spider.entity.biz.XhsUserDetail;
import com.terren.spider.entity.biz.ZhihuContent;
import com.terren.spider.entity.biz.ZhihuUser;
import com.terren.spider.entity.common.SentimentTag;
import com.terren.spider.entity.core.Entry;
import com.terren.spider.entity.core.SpiderKeyword;
import com.terren.spider.util.BasicUtil;
import com.terren.spider.util.common.CommonUtil;
import com.terren.spider.util.common.DateUtil;
import com.terren.spider.util.common.JSONUtil;
import com.terren.spider.util.common.LogUtil;
import com.terren.spider.util.common.Lookup3Hash;
import com.terren.spider.util.common.NLPClient;
import com.terren.spider.util.common.StringUtil;

import us.codecraft.webmagic.selector.Json;

public class HtmlSaveUtil extends BasicUtil {
	private List<WebTestResult> webTestItems = new ArrayList<>();
	private ArticleSearchService articleSearchService;
	private ArticleService articleService;
	private ErshoufangService ershoufangService;
	private ChengjiaofangService chengjiaofangService;
	private ElectronicCommerceService electronicCommerceService;
	private AppDataService appDataService;
	private ArticleTopicService articleTopicService;
	private AppXhsService appXhsService;
	private CommentsService commentsService;
	private ZhihuUserService zhihuUserService;
	private TiebaUserService tiebaUserService;
	private ZhihuContentService zhihuContentService;
	private SpiderKeywordService spiderKeywordService;
	private DouyinService douyinService;
	
	@SuppressWarnings("unchecked")
	public <T> void saveBatch(List<T> items, Entry entry) throws Exception {
		if (!items.isEmpty()) {
			/**
			 * 搜索引擎
			 */
			if (items.get(0) instanceof ArticleSearch) {
				saveArticleSearch((List<ArticleSearch>) items, entry);

			}
			/**
			 * 二手房
			 */
			if (items.get(0) instanceof Ershoufang) {
				saveErshoufang((List<Ershoufang>) items, entry);
			}
			/**
			 * 成交房
			 */
			if (items.get(0) instanceof Chengjiaofang) {
				saveChengjiaofang((List<Chengjiaofang>) items, entry);
			}
			/**
			 * 测试
			 */
			if (items.get(0) instanceof WebTestResult) {
				webTestItems.addAll((Collection<? extends WebTestResult>) items);
			}
			/**
			 * 新闻
			 */
			if (items.get(0) instanceof Article) {
				saveArticles((List<Article>) items, entry);
			}
			/**
			 * 电子商务
			 */
			if (items.get(0) instanceof ElectronicCommerce) {
				saveElectronicCommerce((List<ElectronicCommerce>) items, entry);
			}

			/**
			 * app应用
			 */
			if (items.get(0) instanceof AppData) {
				saveAppData((List<AppData>) items, entry);
			}
			/**
			 * app小红书 （根据关键词搜索）
			 */
			if (items.get(0) instanceof DetailXhs) {
				saveXhsData((List<DetailXhs>) items, entry);
			}
			/**
			 * app小红书 （获取用户信息）
			 */
			if (items.get(0) instanceof XhsUserDetail) {
				saveXhsUserDetail((List<XhsUserDetail>) items, entry);
			}
			/**
			 * 贴吧用户
			 */
			if (items.get(0) instanceof TiebaUser) {
				saveTiebaUserDetail((List<TiebaUser>) items, entry);
			}
			/**
			 * 澎湃新闻app （澎湃新闻 抓评论）
			 */
			if (items.get(0) instanceof UnionComments) {
				saveUnionComments((List<UnionComments>) items, entry);
			}
			/**
			 * 澎湃新闻app （澎湃新闻 评论用户的信息）
			 */
			if (items.get(0) instanceof UnionUserInfo) {
				saveUnionUserInfo((List<UnionUserInfo>) items, entry);
			}
			/**
			 * 抖音app （根据关键词查挑战信息）
			 */
			if (items.get(0) instanceof Douyin) {
				saveDouyin((List<Douyin>) items, entry);
			}
			/**
			 * 文章主贴
			 */
			if (items.get(0) instanceof ArticleTopic) {
				saveArticleTopics((List<ArticleTopic>) items, entry);
			}
			/**
			 * 文章跟帖
			 */
			if (items.get(0) instanceof ArticleReply) {
				saveArticleReply((List<ArticleReply>) items, entry);
			}
			/**
			 * 文章跟帖
			 */
			if (items.get(0) instanceof ZhihuUser) {
				saveZhihuUser((List<ZhihuUser>) items, entry);
			}
			/**
			 * 知乎大v文章回帖爬取
			 */
			if (items.get(0) instanceof ZhihuContent) {
				saveZhihuContent((List<ZhihuContent>) items, entry);
			}
		}

	}
	/**
	 *知乎大v文章回帖爬取
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	public void saveZhihuContent(List<ZhihuContent> items, Entry entry) throws Exception {
		zhihuContentService = CommonUtil.getBean(ZhihuContentService.class);
		List<ZhihuContent> list = new ArrayList<ZhihuContent>();
		for(ZhihuContent item:items){
			if(item.getContent()!=null){
				List<String> jsonList = JSONUtil.selectList(item.getContent(),"$");
				for(String str:jsonList){
					String contentIdStr = JSONUtil.select(str,"$.id");
					if(contentIdStr !=null){
						ZhihuContent zhihu = new ZhihuContent();
						zhihu.setContent(str);
						zhihu.setContentId(Integer.parseInt(contentIdStr));
						
						zhihu.setUserId(entry.getKeyword());
						zhihu.setUpdateTime(System.currentTimeMillis());
						list.add(zhihu);
					}
					
				}
				
			}
		}
		zhihuContentService.saveArticles(list);
		// 处理时间，时间字符串转换成时间
		/*for (ZhihuContent as : (List<ZhihuContent>) items) {
			// 时间字符串转换成时间格式
			ArticleSearch asid = new ArticleSearch();
			String content = StringUtil.truncateAsText(as.getContent());
			as.setContent(content);
			if (null == as.getPubTime() && as.getPubTimeStr() != null) {
				as.setPubTime(DateUtil.formatDateForSeach(as.getPubTimeStr()));
			}
			String act = entry.getAct();
			switch (act) {
			case "zhihuUser"://知乎普通用户文章及回答
				if (StringUtils.contains(as.getUrl(), "answer")) {
					as.setType(0);
				}else {
					as.setType(1);
				}
				break;
			case "zhihuOrg"://知乎企业用户文章及回答
				if (StringUtils.contains(as.getUrl(), "answer")) {
					as.setType(0);
				}else {
					as.setType(1);
				}
				
				break;
			default:
				break;
			}
			as.setAuthorHash(Lookup3Hash.toID(entry.getKeyword()));
			if (null!=content && !content.equals("")) {
				setSentimentTag(as, content);
				// 关键词出现次数
				int occurance = StringUtil.getWordOccurance(content, entry.getKeyword());
				asid.setOccurance(occurance);
			}
			
		}*/
	
	}
	/**
	 * 文章搜索
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	public void saveArticleSearch(List<ArticleSearch> items, Entry entry) throws Exception {
		articleSearchService = CommonUtil.getBean(ArticleSearchService.class);
		List<TiebaUser> authors = new ArrayList<TiebaUser>();
		// 处理时间，时间字符串转换成时间
		for (ArticleSearch as : (List<ArticleSearch>) items) {
			// 时间字符串转换成时间格式
			
			String content = StringUtil.truncateAsText(as.getContent());
			as.setContent(content);
			if (null == as.getPubTime() && as.getPubTimeStr() != null) {
				as.setPubTime(DateUtil.formatDateForSeach(as.getPubTimeStr()));
			}
			String act = entry.getAct();
			switch (act) {
			case "tiebaSearch"://贴吧搜索
				TiebaUser tu = new TiebaUser();
				tu.setUrl(as.getAuthorUrl());
				tu.setUrlId(tu.getUrlId());
				tu.setName(as.getAuthor());
				authors.add(tu);
				if(as.getTitle()!=null && as.getTitle().contains("回复")){
					as.setMasterpaste(0);
				}else{
					as.setMasterpaste(1);
				}
				break;
			case "zhihuUser"://知乎普通用户文章及回答
				if (StringUtils.contains(as.getUrl(), "answer")) {
					as.setMasterpaste(0);
				}else {
					as.setMasterpaste(1);
				}
				break;
			case "zhihuOrg"://知乎企业用户文章及回答
				if (StringUtils.contains(as.getUrl(), "answer")) {
					as.setMasterpaste(0);
				}else {
					as.setMasterpaste(1);
				}
				
				break;
			case "dizo"://迪族车网作者来源发布时间解析
				if(as.getNewsSource()!=null && !"".equals(as.getNewsSource())){
					try{
						String str[] = as.getNewsSource().split("    ");
						as.setPubTime(DateUtil.formatDateString(str[0].trim(), "'时间:'yyyy-MM-dd HH:mm","ZH"));
						as.setNewsSource(str[1].trim());
						as.setAuthor(str[2].trim());
					}catch(Exception e){
						LogUtil.logger.info("迪族车网作者、来源、发布时间解析出错- act:dizo");
					}
					
				}
				
				break;
			default:
				break;
			}
			
			as.setCatagoryId(entry.getCatagoryId());
			as.setSourceId(entry.getSourceId());
			as.setTaskId(entry.getTaskId());
			as.setAuthorHash(Lookup3Hash.toID(entry.getKeyword()));
			if (null!=content && !content.equals("")) {
				setSentimentTag(as, content);
				// 关键词出现次数
				int occurance = StringUtil.getWordOccurance(content, entry.getKeyword());
				as.setOccurance(occurance);
			}
		
		}
		articleSearchService.save((List<ArticleSearch>) items);
		articleSearchService.saveDetail((List<ArticleSearch>) items);
		if(authors.size()!=0 && entry.getAct().equals("tiebaSearch")){
			tiebaUserService = CommonUtil.getBean(TiebaUserService.class);
			tiebaUserService.save(authors);
		}
	}

	/**
	 * 交流性文章主贴
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	public void saveArticleTopics(List<ArticleTopic> items, Entry entry) throws Exception {
		articleTopicService = CommonUtil.getBean(ArticleTopicService.class);
		articleSearchService = CommonUtil.getBean(ArticleSearchService.class);
		List<ArticleSearch> searchList = new ArrayList<ArticleSearch>();
		// 添加t_search
		for (ArticleTopic as : (List<ArticleTopic>) items) {
			ArticleSearch search = new ArticleSearch();//关联信息
			search.setTaskId(entry.getTaskId());
			search.setSourceId(entry.getSourceId());
			search.setUrlId(as.getUrlId());
			search.setCatagoryId(entry.getCatagoryId());
			searchList.add(search);	
		}
		articleSearchService.save((List<ArticleSearch>) searchList);
		articleTopicService.save((List<ArticleTopic>) items);
	}

	/**
	 * 小红书app
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	public void saveXhsData(List<DetailXhs> items, Entry entry) throws Exception {
		appXhsService = CommonUtil.getBean(AppXhsService.class);
		articleSearchService = CommonUtil.getBean(ArticleSearchService.class);
		List<ArticleSearch> searchList = new ArrayList<ArticleSearch>();
		List<DetailXhs> xhsList = new ArrayList<DetailXhs>();
		// 添加t_search
		for (DetailXhs as : (List<DetailXhs>) items) {
			if (!"".equals(as.getId()) && as.getId() != null) {
				ArticleSearch search = new ArticleSearch();
				search.setTaskId(entry.getTaskId());
				search.setSourceId(entry.getSourceId());
				search.setUrlId(as.getUrlId());
				search.setCatagoryId(entry.getCatagoryId());
				// 关键词出现次数
				int occurance = StringUtil.getWordOccurance(as.getDesc(), entry.getKeyword());
				search.setOccurance(occurance);
				searchList.add(search);
				xhsList.add(as);
			}
		}
		appXhsService.addDetailXhs(xhsList);
		articleSearchService.save(searchList);
	}

	/**
	 * 小红书app（获取用户信息）
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	public void saveXhsUserDetail(List<XhsUserDetail> items, Entry entry) throws Exception {
		appXhsService = CommonUtil.getBean(AppXhsService.class);
		appXhsService.saveXhsUserDetail(items);
	}
	/**
	 * 贴吧用户信息
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	public void saveTiebaUserDetail(List<TiebaUser> items, Entry entry) throws Exception {
		tiebaUserService = CommonUtil.getBean(TiebaUserService.class);
		tiebaUserService.saveDetail(items);
	}
	/**
	 * 澎湃新闻app （澎湃新闻 抓评论）
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	public void saveUnionComments(List<UnionComments> items, Entry entry) throws Exception {
		commentsService = CommonUtil.getBean(CommentsService.class);
		String hotcommentsJson = null;
		for (int i = 0; i < items.size(); i++) {
			if (items.get(i).getHotcomments() != null) {
				hotcommentsJson = items.get(i).getHotcomments();
				commentsService.saveUnionComments(getJsonData(hotcommentsJson));
			}
		}
	}

	/**
	 * 澎湃新闻app （澎湃新闻 评论用户的信息）
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	public void saveUnionUserInfo(List<UnionUserInfo> items, Entry entry) throws Exception {
		commentsService = CommonUtil.getBean(CommentsService.class);
		List<UnionUserInfo> userinfoList = new ArrayList<>();
		for (int i = 0; i < items.size(); i++) {
			items.get(i).setSourceid(18);
			items.get(i).setDbType(DBSourceManager.otherDBSource);
			userinfoList.add(items.get(i));
		}
		commentsService.saveUnionUserInfo(userinfoList);
	}

	/**
	 * 解析json
	 */
	private List<UnionComments> getJsonData(String hotcommentsJson) {
		List<UnionComments> commentsList = new ArrayList<>();
		//hotcomments的节点下，当有多条时，就是一个JSONArray；有一条时，就是一个JSONObject，所以要判断一下
		if (hotcommentsJson.startsWith("[")) {
			JSONArray jsonAry = JSON.parseArray(hotcommentsJson);
			for (int i = 0; i < jsonAry.size(); i++) {
				// 用了一下webmagic的转jsonpath的方法，感觉还不错
				Json json = new Json(jsonAry.getString(i));
				UnionComments unionComments = new UnionComments();

				unionComments.setSourceid(18);
				unionComments.setUid(Long.parseLong(json.jsonPath("$.userInfo.userId").get()));
				unionComments.setUrlid(Lookup3Hash.toID(json.jsonPath("$.commentId").get()));
				unionComments.setReplyid(json.jsonPath("$.commentId").get());
				unionComments.setReplycontent(json.jsonPath("$.content").get());
				unionComments.setPubtime(DateUtil.formatDateToSecond(json.jsonPath("$.pubTime").get()).toString());
				unionComments.setAgreecount(Integer.parseInt(json.jsonPath("$.praiseTimes").get()));
				unionComments.setDbType(DBSourceManager.otherDBSource);
				commentsList.add(unionComments);
			}
		}else{
			Json json = new Json(hotcommentsJson);
			UnionComments unionComments = new UnionComments();
			unionComments.setSourceid(18);
			unionComments.setUid(Long.parseLong(json.jsonPath("$.userInfo.userId").get()));
			unionComments.setUrlid(Lookup3Hash.toID(json.jsonPath("$.commentId").get()));
			unionComments.setReplyid(json.jsonPath("$.commentId").get());
			unionComments.setReplycontent(json.jsonPath("$.content").get());
			unionComments.setPubtime(DateUtil.formatDateToSecond(json.jsonPath("$.pubTime").get()).toString());
			unionComments.setAgreecount(Integer.parseInt(json.jsonPath("$.praiseTimes").get()));
			unionComments.setDbType(DBSourceManager.otherDBSource);
			commentsList.add(unionComments);
		}
		
		return commentsList;
	}

	/**
	 * 抖音app （根据关键词查挑战信息）
	 * 
	 */
	public void saveDouyin(List<Douyin> items, Entry entry) throws Exception {
		douyinService = CommonUtil.getBean(DouyinService.class);
		List<Douyin> addList=new ArrayList<>();
		
		
    	
		for (int i = 0; i < items.size(); i++) {
			JSONArray jsonAry=JSON.parseArray(items.get(i).getChallenge_list());
			System.out.println(jsonAry.size());
			for (int j = 0; j < jsonAry.size(); j++) {
				Douyin douyin=new Douyin();
				
				douyin.setAwemeId(getDouyinId(jsonAry.get(j).toString()));
				douyin.setJson(jsonAry.get(j).toString());
				douyin.setKeyword(entry.getKeyword());
				douyin.setType(2);
				addList.add(douyin);
				douyinService.addDouyin(addList);
			}
			
		}
	}
	
	private Long getDouyinId(String json){
		String a=JSONUtil.select(json, "$.aweme_id");
		return Long.parseLong(a);
		
	}
	/**
	 * 交流性文章跟帖
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	private void saveArticleReply(List<ArticleReply> items, Entry entry) throws Exception {
		articleTopicService = CommonUtil.getBean(ArticleTopicService.class);
		articleSearchService = CommonUtil.getBean(ArticleSearchService.class);
		spiderKeywordService = CommonUtil.getBean(SpiderKeywordService.class);
		List<ArticleSearch> searchList = new ArrayList<ArticleSearch>();
		List<SpiderKeyword> skList = new ArrayList<SpiderKeyword>();
		// 添加t_search
		int i = 1;
		for (ArticleReply as : (List<ArticleReply>) items) {
			ArticleSearch search = new ArticleSearch();
			search.setTaskId(entry.getTaskId());
			search.setSourceId(entry.getSourceId());
			search.setUrlId(as.getUrlId());
			search.setCatagoryId(entry.getCatagoryId());
			searchList.add(search);
			try {
				as.setPubTime(DateUtil.formatDateForSeach(as.getPubTime()));
			} catch (Exception e) {
				as.setPubTime("0000-00-00");
				LogUtil.logger.info("时间转换异常-saveArticleReply");
			}
			if (as.getReplyNum() == null) {
				as.setReplyNum(i);
				i++;
			}
			String usrUrl = as.getAuthorUrl();
			if(usrUrl !=null && !"".equals(usrUrl)){
				String tokenUrl = StringUtils.substringAfterLast(usrUrl, "/");
				if(!"".equals(tokenUrl)&&tokenUrl !=null){
					Long userId = Lookup3Hash.toID(tokenUrl);
					as.setAuthorId(userId);//把类似用户shen-che-dang的url_token 保存到回帖表中
					//--保存用户信息到t_spider_keyword
					SpiderKeyword sk = new SpiderKeyword();
					sk.setHashId(userId);
					sk.setKeyword(tokenUrl);
					if(usrUrl.contains("people")&&!tokenUrl.contains("people")){
						sk.setSourceId(1014);
					}else{
						sk.setSourceId(1015);
					}
					sk.setStatus(0);
					sk.setFetchtimes(0);
					skList.add(sk);
				}
			}
			
			
			
		}
		if(skList.size()!=0){
			spiderKeywordService.save(skList);
		}
		articleSearchService.save((List<ArticleSearch>) searchList);
		articleTopicService.saveReply((List<ArticleReply>) items);
	}

	/**
	 * 知乎用户信息
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	private void saveZhihuUser(List<ZhihuUser> items, Entry entry) throws Exception {
		zhihuUserService = CommonUtil.getBean(ZhihuUserService.class);
		List<ZhihuUser> userList = new ArrayList<ZhihuUser>();
		List<ZhihuUser> userDetailList = new ArrayList<ZhihuUser>();
		for(ZhihuUser zhihuUser : items){
			if(zhihuUser.getUserInfo()!=null){
				String userInfo = JSONUtil.select(zhihuUser.getUserInfo(), "$.entities.users");
				if(userInfo !=null){
					ZhihuUser user = new ZhihuUser();
					user.setUserId(entry.getKeyword());
					user.setUpdateTime(System.currentTimeMillis());
					user.setUserInfo(userInfo);
					userList.add(user);
					//----详情
					user.setAuthorHash(Lookup3Hash.toID(entry.getKeyword()));
					user.setAuthor(JSONUtil.select(userInfo, entry.getKeyword()+".name"));
					user.setHeadline(JSONUtil.select(userInfo, entry.getKeyword()+".headline"));
					String answerCount = JSONUtil.select(userInfo, entry.getKeyword()+".answerCount");
					if(answerCount!=null){
						user.setAnswerNote(Integer.parseInt(answerCount));
					}
					String articlesCount = JSONUtil.select(userInfo, entry.getKeyword()+".articlesCount");
					if(articlesCount!=null){
						user.setArticlesCount(Integer.parseInt(articlesCount));
					}
					String voteupCount = JSONUtil.select(userInfo, entry.getKeyword()+".voteupCount");
					if(voteupCount!=null){
						user.setAgreeCount(Integer.parseInt(answerCount));
					}
					String thankedCount = JSONUtil.select(userInfo, entry.getKeyword()+".thankedCount");
					if(thankedCount!=null){
						user.setThanksCount(Integer.parseInt(thankedCount));
					}
					String favoritedCount = JSONUtil.select(userInfo, entry.getKeyword()+".favoritedCount");
					if(favoritedCount!=null){
						user.setCollectCount(Integer.parseInt(answerCount));
					}
					String logsCount = JSONUtil.select(userInfo, entry.getKeyword()+".logsCount");
					if(logsCount!=null){
						user.setPublicEditCount(Integer.parseInt(logsCount));
					}
					String followerCount = JSONUtil.select(userInfo, entry.getKeyword()+".followerCount");
					if(followerCount!=null){
						user.setFollowedCount(Integer.parseInt(followerCount));
					}
					userDetailList.add(user);
				}
				
			}
			
		/*for (ZhihuUser zhihuUser : items) {
			zhihuUser.setAuthorId(entry.getKeyword());
			zhihuUser.setSourceId(entry.getSourceId());
			zhihuUser.setAuthorHash(Lookup3Hash.toID(entry.getKeyword()));
			if(zhihuUser.getCollectCountStr()!=null&&!"".equals(zhihuUser.getCollectCount())){
				String ubch= StringUtils.substringAfter(zhihuUser.getCollectCountStr(), " ，");
				Integer ubchNum = Integer.parseInt(StringUtil.getNumbers(ubch));
				zhihuUser.setCollectCount(ubchNum);
			}
			String act = entry.getAct();
			switch (act) {
			case "zhihuUser"://知乎普通用户 
				if(zhihuUser.getCollectCountStr()!=null&&!"".equals(zhihuUser.getCollectCount())){
					String ubch= StringUtils.substringAfter(zhihuUser.getCollectCountStr(), " ，");
					Integer ubchNum = Integer.parseInt(StringUtil.getNumbers(ubch));
					zhihuUser.setCollectCount(ubchNum);
				}
				break;
			case "zhihuOrg"://知乎企业用户 
				if(zhihuUser.getCollectCountStr()!=null&&!"".equals(zhihuUser.getCollectCount())){
					String ubch= StringUtils.substringAfter(zhihuUser.getCollectCountStr(), " ，");
					Integer ubchNum = Integer.parseInt(StringUtil.getNumbers(ubch));
					zhihuUser.setCollectCount(ubchNum);
				}
				break;
			default:
				break;
			}*/
		}
		zhihuUserService.saveUserInfos(userList);
		zhihuUserService.save(userDetailList);
	}
	
	/**
	 * 新闻类
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	public void saveArticles(List<Article> items, Entry entry) throws Exception {
		articleService = CommonUtil.getBean(ArticleService.class);
		for (Article article : items) {
			article.setContent(StringUtil.truncateAsText(article.getContent()));
			article.setHtml(null);
			article.setEntryId(entry.getEntryId());
			article.setSourceId(entry.getSourceId());
			article.setCatagoryId(entry.getCatagoryId());
		}
		articleService.save(items);
	}

	/**
	 * 成交房
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	public void saveChengjiaofang(List<Chengjiaofang> items, Entry entry) throws Exception {
		List<Chengjiaofang> chengjiaoList = (List<Chengjiaofang>) items;
		chengjiaofangService = CommonUtil.getBean(ChengjiaofangService.class);
		for (int i = 0; i < chengjiaoList.size(); i++) {
			chengjiaoList.get(i).setHouseId(
					(Lookup3Hash.toID(chengjiaoList.get(i).getInfoId() + chengjiaoList.get(i).getSourceId())));
			chengjiaoList.get(i).setSourceId(entry.getSourceId() + "");
		}
		chengjiaofangService.save(chengjiaoList);
	}

	/**
	 * 二手房
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	public void saveErshoufang(List<Ershoufang> items, Entry entry) throws Exception {
		List<Ershoufang> ershoufangList = (List<Ershoufang>) items;
		ershoufangService = CommonUtil.getBean(ErshoufangService.class);
		for (int i = 0; i < ershoufangList.size(); i++) {
			if (ershoufangList.get(i).getInfoId() != "" && ershoufangList.get(i).getInfoId() != null) {
				ershoufangList.get(i).setSpiderDate(DateUtil.dateToString(new Date(), "yyyy-MM-dd"));
				;
				ershoufangList.get(i).setHouseId((Lookup3Hash.toID(ershoufangList.get(i).getInfoId()
						+ ershoufangList.get(i).getSourceId() + ershoufangList.get(i).getSpiderDate())));
				ershoufangList.get(i).setSourceId(entry.getSourceId() + "");
				ershoufangList.get(i).setInfoId(ershoufangList.get(i).getInfoId().replaceAll("[^0-9]", ""));
			}

		}
		ershoufangService.save(ershoufangList);
	}

	/**
	 * 电子商务
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	public void saveElectronicCommerce(List<ElectronicCommerce> items, Entry entry) throws Exception {
		List<ElectronicCommerce> list = (List<ElectronicCommerce>) items;
		electronicCommerceService = CommonUtil.getBean(ElectronicCommerceService.class);
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setEntryId(entry.getEntryId());
			list.get(i).setSourceId(entry.getSourceId());
			list.get(i).setCatagoryId(entry.getCatagoryId());
		}
		electronicCommerceService.save(list);
	}

	/**
	 * app应用
	 * 
	 * @param items
	 * @param entry
	 * @throws Exception
	 */
	protected void saveAppData(List<AppData> items, Entry entry) throws Exception {
		List<AppData> list = (List<AppData>) items;
		appDataService = CommonUtil.getBean(AppDataService.class);
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setEntryId(entry.getEntryId());
			list.get(i).setSourceId(entry.getSourceId());
			list.get(i).setCatagoryId(entry.getCatagoryId());
		}
		appDataService.save(list);

	}

	/**
	 * 情感分析
	 * 
	 * @param pojo
	 * @param content
	 */
	protected <T> void setSentimentTag(T pojo, String content) {
		String sentiment;
		String tag;
		try {
			SentimentTag result = NLPClient.getSentimentTag(content);
			sentiment = result.Sentiment != null ? result.Sentiment : "";// 情感倾向
			tag = result.Tag != null ? result.Tag : "";// 文章观点
		} catch (Exception e) {
			sentiment = "";
			tag = "";
			log.error("saveArticleSearch error", e);
		}
		if (pojo instanceof ArticleSearch) {
			((ArticleSearch) pojo).setSentiment(sentiment);
			((ArticleSearch) pojo).setTag(tag);
			((ArticleSearch) pojo).setSentianalysis(1);
		}
	}

	public String getWebTestJson() {
		return JSONUtil.toJSONString(webTestItems);
	}

	public List<WebTestResult> getWebTestItems() {
		return webTestItems;
	}

	public void setWebTestItems(List<WebTestResult> webTestItems) {
		this.webTestItems = webTestItems;
	}
}
